package com.guigu.www.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.node.IntNode;
import com.guigu.www.pojo.Menus;

import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.Roles;
import com.guigu.www.service.MenusService;
import com.guigu.www.service.RolesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MenusController {
    //获取menusService
    @Autowired
    MenusService menusService;
    //获取角色service
    @Autowired
    RolesService rolesService;
    //查询父菜单
    @RequestMapping("/queryByMenusMenuid.action")
    public List<Menus> queryByMenusMenuid(HttpSession session){
        //查询当前角色拥有的角色
        String username = (String) session.getAttribute("username");
        List<Integer> roleid=rolesService.querryByusersRoleByusername(username);
        System.out.println("真好"+roleid);
        List<Menus> list = menusService.queryByMenusFuId(roleid,0);
        return list;
    }

    //查询子菜单
    @RequestMapping("/queryByMenusSon.action")
    public List<Menus> queryByMenusSon(int menuseg,HttpSession session){
        String username = (String) session.getAttribute("username");
        List<Integer> roleid=rolesService.querryByusersRoleByusername(username); System.out.println("真好"+username);
        System.out.println("真好"+roleid);
        //获取方法
        List<Menus> list=menusService.queryByMenusSon(roleid,menuseg);
        return list;
    }
    //查询所以分页
    @RequestMapping("querymenus.action")
    public PageVo<Menus> querymenus(Menus menus,
                                    @RequestParam(value = "pagenum",defaultValue = "1")int pagenum,
                                    @RequestParam(value = "pagesize",defaultValue = "5")int pagesize){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(StringUtils.isNotBlank(menus.getName())){
            queryWrapper.like("name",menus.getName());
        }
        Page<Menus> page=menusService.page(new Page<Menus>(pagenum,pagesize),queryWrapper);
        PageVo<Menus> pageVo=new PageVo<>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
    @RequestMapping("addmenus.action")
    public Map addmenus(Menus menus){
        Map map=new HashMap();
        boolean res=menusService.save(menus);
        if(res){
            map.put("msg","添加成功");
        }else {
            map.put("msg","添加失败");
        }
        return map;
    }
    @RequestMapping("deletemenus.action/{id}")
    public Map deletemenus(@PathVariable int id){
        Map map=new HashMap();
        boolean res=menusService.removeById(id);
        if(res){
            map.put("msg","删除成功");
        }else {
            map.put("msg","删除失败");
        }
        return map;
    }
    @RequestMapping("updatemenus.action")
    public Map updatemenus(Menus menus){
        Map map=new HashMap();
        boolean res=menusService.updateById(menus);
        if(res){
            map.put("msg","修改成功");
        }else {
            map.put("msg","修改失败");
        }
        return map;
    }
    @RequestMapping("querybymenusid.action/{id}")
    public Menus querybymenusid(@PathVariable int id){
        Menus res=menusService.getById(id);
        return res;
    }
    //查询最大的排序号
    @RequestMapping("querymaxseq")
    public int querymaxseq(){
        return menusService.querymaxseq();
    }
}
